{#
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
#}

{% extends "airflow/dag.html" %}
{% from 'appbuilder/loading_dots.html' import loading_dots %}

{% block page_title %}{{ dag.dag_id }} - Graph - {{ appbuilder.app_name }}{% endblock %}

{% block head_meta %}
  {{ super() }}
  <meta name="dag_run_id" content="{{ dag_run_id }}">
  <meta name="execution_date" content="{{ execution_date }}">
  <meta name="arrange" content="{{ arrange }}">
  <meta name="task_instances_url" content="{{ url_for('Airflow.task_instances') }}">
{% endblock %}

{% block head_css %}
  {{ super() }}
  <link rel="stylesheet" type="text/css" href="{{ url_for_asset('graph.css') }}">
  <style type="text/css">
    {% for state, state_color in state_color_mapping.items() %}
      g.node.{{state}} rect {
        stroke: {{state_color}};
      }
    {% endfor %}
  </style>
{% endblock %}

{% block content %}
  {{ super() }}
  <div class="row dag-view-tools">
    <div class="col-md-10">
      <form method="get" class="form-inline">
        <input type="hidden" name="root" value="{{ root }}">
        <input type="hidden" value="{{ dag.dag_id }}" name="dag_id">
        <input type="hidden" name="_csrf_token" value="{{ csrf_token() }}">
        <div class="form-group">
          <label class="sr-only" for="base_date">Base date</label>
          <div class="input-group">
            {{ form.base_date(class_="form-control", disabled=not(dag.has_dag_runs())) }}
          </div>
        </div>
        <div class="form-group">
          <label class="sr-only" for="num_runs">Number of runs</label>
          <div class="input-group">
            <div class="input-group-addon">Runs</div>
            {{ form.num_runs(class_="form-control", disabled=not(dag.has_dag_runs())) }}
          </div>
        </div>
        <div class="form-group">
          <label class="sr-only" for="execution_date">Run</label>
          <div class="input-group">
            <div class="input-group-addon">Run</div>
            {{ form.execution_date(class_="form-control", disabled=not(dag.has_dag_runs())) }}
          </div>
        </div>
        <div class="form-group">
          <label class="sr-only" for="arrange">Layout</label>
          <div class="input-group">
            <div class="input-group-addon">Layout</div>
            {{ form.arrange(class_="form-control") }}
          </div>
        </div>
        <button type="submit" class="btn">Update</button>
        {% if not dag.has_dag_runs() %}<span class="text-warning" style="margin-left:16px;">No DAG runs yet.</span>{% endif %}
      </form>
    </div>
    <div class="col-md-2 text-right">
      <label class="sr-only" for="searchbox">Search</label>
      <input type="search" class="form-control" id="searchbox" placeholder="Find Task…">
    </div>
  </div>

  <div class="legend-row">
    <div>
      {% for op in operators %}<span class="legend-item" style="color: {{ op.ui_fgcolor }};background: {{ op.ui_color }};">
        {{ op.task_type }}</span>{% endfor %}
    </div>
    <div>
      {% for state, state_color in state_color_mapping.items() %}
        <span class="legend-item legend-item--interactive js-state-legend-item" data-state="{{state}}" style="border-color: {{state_color}};">
          {{state}}
        </span>
      {% endfor %}
      <span class="legend-item legend-item--interactive legend-item--no-border js-state-legend-item" data-state="no_status" style="border-color:white;">
        no_status
      </span>
    </div>
  </div>
  <div id="error" style="display: none; margin-top: 10px;" class="alert alert-danger" role="alert">
    <span class="material-icons" aria-hidden="true">error</span>
    <span id="error_msg">Oops.</span>
  </div>
  <br>
  <div class="refresh-actions">
    {{ loading_dots(id='loading-dots', classes='refresh-loading') }}
    <label class="switch-label">
      <input class="switch-input" id="auto_refresh" type="checkbox" {% if dag_run_state == 'running' %}checked{% endif %}>
      <span class="switch" aria-hidden="true"></span>
      Auto-refresh
    </label>
    <button class="btn btn-default btn-sm" id="refresh_button">
      <span class="material-icons" aria-hidden="true">refresh</span>
    </button>
  </div>
  <div class="svg-wrapper">
    <div class="graph-svg-wrap">
      <svg id="graph-svg" viewBox="0 0 100 100">
        <g id="dig" transform="translate(20,20)"></g>
      </svg>
    </div>
  </div>
{% endblock %}

{% block tail %}
  {{ super() }}
  <script>
    const nodes = {{ nodes|tojson }};
    const edges = {{ edges|tojson }};
    const tasks = {{ tasks|tojson }};
    let taskInstances = {{ task_instances|tojson }};
    const autoRefreshInterval = {{ auto_refresh_interval }};
    const priority = {{ state_priority|tojson }};
  </script>
  <script src="{{ url_for_asset('d3.min.js') }}"></script>
  <script src="{{ url_for_asset('dagre-d3.min.js') }}"></script>
  <script src="{{ url_for_asset('d3-shape.min.js') }}"></script>
  <script src="{{ url_for_asset('d3-tip.js') }}"></script>
  <script src="{{ url_for_asset('graph.js') }}"></script>
{% endblock %}
